<template>
  <div>
    <el-form ref="form" :model="formData" label-width="80px">
      <el-form-item label="商品名称">
        <el-input v-model="formData.name" placeholder="你想输入什么"></el-input>
      </el-form-item>

      <el-form-item label="商品分类">
        <el-select v-model="formData.category" placeholder="请选择动物">
          <el-option label="动物" value="动物"></el-option>
          <el-option label="人" value="人"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="商品价格">
        <el-input-number
          v-model="formData.price"
          @change="handleChange"
          :min="1"
          :max="9999999999"
          label="描述文字"
        ></el-input-number>
      </el-form-item>

      <el-form-item label="商品图片">
        <el-upload
          class="avatar-uploader"
          action="http://sell.h5.itsource.cn:8087/goods/goods_img_upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="商品描述">
        <el-input
          type="textarea"
          v-model="formData.goodsDesc"
          placeholder="你想输入什么"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即添加</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapActions, mapState } from "vuex";
import { accountAddx,goodsImg } from "@/api/user";

export default {
  data() {
    return {
      imageUrl: "",
      formData: {
        name: "小龙猫",
        category: "动物",
        price: "",
        imgUrl: "",
        goodsDesc: "图片测试",
      },
    };
  },
  methods: {
    ...mapActions("user", ["accountAddx",'goodsImg']),

  async onSubmit() {
      let res =  await accountAddx(this.formData);
      let { data, total, msg } = res.data;
        this.$router.push('/goods/goods-list') 
    },
   
    handleChange(value) {
      console.log(value);
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.formData.imgUrl=res.imgUrl
    },

    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style lang="scss" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
